package Interface.Game;

import Interface.Player.JDialogPlayerStats;
import Jastermind.Appli;
import Jastermind.Attempt;
import Jastermind.Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JPanelGame extends JPanel {

    private Game game;
    private JPanel jpCombinaisonPlayer;
    private JPanel jpCombinaisonResult;
    private JButton jbtOk;
    private final int jElementWidth = 20;
    private final int jElementHeight = 20;
    private int currentAttempt;

    public JPanelGame(Game game) {
        super();
        this.game = game;
        this.setLayout(null);

        Appli.getJfMain().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        Appli.getJfMain().setSize((game.getPlayer().getCombinaisonSize() + 2) * (jElementWidth + 5) + 10 * game.getPlayer().getCombinaisonSize() + 15, game.getPlayer().getNbEssais() * (jElementHeight + 5) + 100);

        JButton playerInfo = new JButton("Stats");
        playerInfo.addActionListener(new ActionJPGame());
        playerInfo.setBounds(5+(game.getCombinaison().size()-4)*12, Appli.getJfMain().getHeight() - 80, 90, jElementHeight);
        this.add(playerInfo);

        JButton cancelGame = new JButton("Abandon");
        cancelGame.addActionListener(new ActionJPGame());
        cancelGame.setBounds(playerInfo.getX()+90 + 10, Appli.getJfMain().getHeight() - 80, 90, jElementHeight);
        this.add(cancelGame);

        if (!game.getListAttemptsDone().isEmpty()) {
            for (int iPastAttempt = 0; iPastAttempt < game.getListAttemptsDone().size(); iPastAttempt++) {
                showCurrentAttempt(iPastAttempt);
                showResultAttempt(game.getListAttemptsDone().get(iPastAttempt));
            }
            currentAttempt = game.getListAttemptsDone().size() - 1;
        }
        else {
            currentAttempt = 0;
        }
        showCurrentAttempt(currentAttempt);
        
//        Afficher la combinaison pour l'examen
//        for(int iColor=game.getCombinaison().size()-1;iColor>=0;iColor--){
//          JLabel jblColor=new JLabel();
//          jblColor.setOpaque(true);
//          jblColor.setBounds(120-iColor*30,10, 20, 20);
//          jblColor.setBackground(game.getCombinaison().get(iColor));
//          this.add(jblColor);
//        }

    }

    public Game getGame() {
        return game;
    }
    
    private void showCurrentAttempt(int iAttempt) {
        //Dimensions
        int posYEssai = Appli.getJfMain().getHeight() - 80 - (iAttempt + 1) * (jElementHeight + 5);
        int posXNbEssai = 5;
        int posXCombinaisonPart = posXNbEssai + jElementWidth;
        int posXOkButton = posXCombinaisonPart + game.getPlayer().getCombinaisonSize() * (jElementWidth + 5) + 5;
        int posXCombinaisonResult = posXOkButton + (jElementWidth + 5);

        JLabel jblNbEssai = new JLabel("" + (iAttempt + 1));
        jblNbEssai.setBounds(posXNbEssai, posYEssai, jElementWidth - 5, jElementHeight);
        this.add(jblNbEssai);

        jpCombinaisonPlayer = new JPanel();
        jpCombinaisonPlayer.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
        jpCombinaisonPlayer.setBounds(posXCombinaisonPart, posYEssai, posXOkButton - posXCombinaisonPart, jElementHeight);

        for (int iColor = 0; iColor < game.getPlayer().getCombinaisonSize(); iColor++) {
            JButton jbtCombinaisonPart = new JButton(new ImageIcon("./images/pasok.gif"));
            jbtCombinaisonPart.setBackground(null);
            jbtCombinaisonPart.setActionCommand("combinaisonColor");
            jbtCombinaisonPart.setPreferredSize(new Dimension(jElementWidth, jElementHeight));
            jbtCombinaisonPart.addActionListener(new ActionJPGame());
            jbtCombinaisonPart.setBorder(BorderFactory.createLineBorder(Color.black));
            jpCombinaisonPlayer.add(jbtCombinaisonPart);
        }
        this.add(jpCombinaisonPlayer);

        jbtOk = new JButton(new ImageIcon("./images/result.gif"));
        jbtOk.setActionCommand("okCombinaison");
        jbtOk.addActionListener(new ActionJPGame());
        jbtOk.setBounds(posXOkButton, posYEssai, jElementWidth, jElementHeight);
        this.add(jbtOk);

        jpCombinaisonResult = new JPanel();
        jpCombinaisonResult.setLayout(new GridLayout(2, 3, 1, 1));
        jpCombinaisonResult.setBounds(posXCombinaisonResult, posYEssai, 10 * game.getPlayer().getCombinaisonSize(), jElementHeight);

        for (int iCombinaisonResult = 0; iCombinaisonResult < game.getPlayer().getCombinaisonSize(); iCombinaisonResult++) {
            JLabel jblCombinaisonResult = new JLabel();
            jblCombinaisonResult.setOpaque(true);
            jblCombinaisonResult.setBackground(Color.WHITE);
            jpCombinaisonResult.add(jblCombinaisonResult);
        }
        this.add(jpCombinaisonResult);
    }

    private void showResultAttempt(Attempt attempt) {
        for (int iColor = attempt.getAttemptResult().size() - 1; iColor >= 0; iColor--) {
            if (attempt.getAttemptResult().get(iColor) != null) {
                ((JLabel) jpCombinaisonResult.getComponent(iColor)).setBackground(attempt.getAttemptResult().get(iColor));
            }
        }
        if (attempt.isIsAttemptWinner() == false) {
            game.getListAttemptsDone().add(attempt);
            if (currentAttempt < game.getPlayer().getNbEssais() - 1) {
                currentAttempt++;
                showCurrentAttempt(currentAttempt);
                this.revalidate();
                this.repaint();
            }
            else {
                new JDialogEndGame(false,game);
            }
        }
        else {
            new JDialogEndGame(true,game);
        }
    }

    private ArrayList<Color> getPlayedColors() {
        ArrayList<Color> playedColors = new ArrayList<Color>();
        jbtOk.setEnabled(false);
        for (int iComponent = jpCombinaisonPlayer.getComponentCount() - 1; iComponent >= 0; iComponent--) {
            ((JButton) jpCombinaisonPlayer.getComponent(iComponent)).setEnabled(false);
            if (((JButton) jpCombinaisonPlayer.getComponent(iComponent)).getIcon() == null) {
                playedColors.add((((JButton) jpCombinaisonPlayer.getComponent(iComponent)).getBackground()));
            }
            else {
                playedColors.add(null);
            }
        }
        return playedColors;
    }

    private boolean oneColorSelected() {
        int iComponent = 0;
        while (iComponent < jpCombinaisonPlayer.getComponentCount() && ((JButton) jpCombinaisonPlayer.getComponent(iComponent)).getIcon() != null) {
            iComponent++;
        }
        return iComponent != jpCombinaisonPlayer.getComponentCount();
    }

    private class ActionJPGame extends WindowAdapter implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("Abandon")) {
                game.abandonGame();
            }
            if(e.getActionCommand().equals("Stats")){
                new JDialogPlayerStats(game.getPlayer());
            }
            if (e.getActionCommand().equals("combinaisonColor")) {
                new JDialogColorChooser(game.getPlayer().getListColors(), (JButton) e.getSource());
            }
            if (e.getActionCommand().equals("okCombinaison")) {
                if (oneColorSelected() == true) {
                    Attempt currentAttempt = new Attempt(game, getPlayedColors());
                    showResultAttempt(currentAttempt);
                }
                else {
                    JOptionPane.showMessageDialog(null, "Vous devez au moins selectionner une couleur");
                }
            }

        }
    }
}

